import React from "react"
import LaunchScreen from "./LaunchScreen";
import WelcomPage from "./WelcomPage";
import MainContentView from "./MainContentView.jsx";
import { get, postFormData } from "../network/request"

import SelectType from "./SelectType";
import WorkType from "./WorkType";
import QuestionPage from "./QuestionPage";

// import {TONConnectPage} from "./TONConnectPage/TONConnectPage";

class MainPage extends React.Component{
    constructor(props){
        super(props)

        // localStorage.clear(); // 清除本地存储

        this.state = {
            showControl : 1,
            backColor: "#0B0D14"
        };

        // 绑定事件处理函数到当前实例  
        this.handleClick = this.handleClick.bind(this);
        this.handTypeClick = this.handTypeClick.bind(this)

        this.handQuestionClick = this.handQuestionClick.bind(this)
        this.handSelectQuestionClick = this.handSelectQuestionClick.bind(this)
    }

    componentDidMount() {
        this.timerId = setTimeout(() => {
            this.initCurrentMainPage();
        }, 800);
    }

    initCurrentMainPage() {
        let initUser = localStorage.getItem("initUser");
        if (initUser == null || initUser === "undefined" || initUser === "") { // 如果没有初始化用户
            this.setState({ showControl: 2 });
        } else {
            // 已经初始化用户
            let userJson = JSON.parse(initUser);
            console.log("已初始化用户:", userJson);
            let selectJobType = userJson["type"];

            // 已经选择职业类型
            if (selectJobType === 1 || selectJobType === 2) {
            // 已经选择职业类型
                let jobsIds = userJson["job_ids"];
                if (jobsIds && jobsIds.length > 0) {
                    this.setState({ showControl: 5 });
                } else {
                    this.setState({ showControl: 4 });
                }
            } else {
                // 未选择职业类型
                this.setState({ showControl: 3 });
            }
            // this.setState({ showControl: 3 });
        }
    }


    componentWillUnmount() {
        // 组件卸载前清除定时器
        if (this.timerId) {
            clearTimeout(this.timerId);
        }
    }

    handleClick() {
        this.setState({ showControl:3});
    } 


    handTypeClick(){
        this.setState({ showControl:4});
    }

    handSelectQuestionClick(){
        this.setState({ showControl:5});
    }


    handQuestionClick(){
        // this.setState({ backColor: "#FFFFFF"});
        this.setState({ showControl:6});
    }

    render(){
        return(
            <div className="MainContentV" style={{background: this.state.backColor}}>
                {this.state.showControl === 1 && <LaunchScreen></LaunchScreen>}
                {this.state.showControl === 2 && <WelcomPage onClick={this.handleClick}></WelcomPage>}

                {/* {this.state.showControl === 3 && <TONConnectPage></TONConnectPage>} */}

                {this.state.showControl === 3 && <SelectType onClick={this.handTypeClick}></SelectType>}
                {this.state.showControl === 4 && <WorkType onClick={this.handSelectQuestionClick} onBackClick={this.handleClick}></WorkType>}

                {this.state.showControl === 5 && <QuestionPage onClick={this.handQuestionClick}></QuestionPage>}
                {/* {this.state.showControl == 6 && <TabBarView></TabBarView>} */}

                {this.state.showControl === 6 && <MainContentView></MainContentView>}
            </div>   
        )
    }
}

export default MainPage